package br.com.vm.service.integration.json.impl;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import br.com.vm.model.Mapa;
import br.com.vm.model.vo.MelhorRotaReq;
import br.com.vm.model.vo.MelhorRotaResp;
import br.com.vm.service.MapaService;
import br.com.vm.service.context.SpringApplicationContext;
import br.com.vm.service.integration.IntegrationService;

@Component
@Path("/mapa/json")
public class IntegrationServiceJSONImpl extends SpringBeanAutowiringSupport
		implements IntegrationService {

	@Resource
	private MapaService mapaService;

	@PostConstruct
	public void init() {
		SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
	}

	@POST
	@Path("/salvarMapa")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response salvarMapa(Mapa poMapa) throws Exception {

		mapaService = (MapaService) SpringApplicationContext
				.getBean("mapaService");

		try {
			mapaService.salvarMapa(poMapa);
		} catch (Exception e) {

			return Response.status(500).entity(e.getMessage()).build();
		}
		return Response.status(200).entity("OK").build();
	}

	@POST
	@Path("/obterMelhorRota")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response obterMelhorRota(MelhorRotaReq poMelhorRotaReq)
			throws Exception {

		mapaService = (MapaService) SpringApplicationContext
				.getBean("mapaService");

		MelhorRotaResp resp = null;

		try {
			resp = mapaService.obterMelhorRota(poMelhorRotaReq);
		} catch (Exception e) {
			return Response.status(500).entity(e.getMessage()).build();
		}
		return Response.status(200).entity(resp).build();

	}
}
